잠시만 기다려 주세요

     '바다아이 사이트는 윤석열 정부 탄핵을 지지합니다.'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (19)  |  여러가지 팁 (1095)  |  추천 및 재미 (163)  |  자료실 (28)  |  
시사, 이슈, 칼럼, 평론, 비평 (801)  |  끄적거림 (142)  |  문예 창작 (719)  |  바람 따라 (75)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    postgresql

postgresql - postgresql 프로시져 함수에서 for, foreach 문 사용하기, 사용법
이 름 : 바다아이   |   조회수 : 11248         짧은 주소 : https://www.bada-ie.com/su/?241591819233
for와 foreach를 사용하여 합계를 구하는 함수를 만들어 보았습니다.

생각대로 foreach 가 훨신 깔끔하게 느껴집니다.
 

-- for를 사용하여 합계 구하기

CREATE OR REPLACE FUNCTION fn_sum(numeric[])
RETURNS numeric AS $$
DECLARE
     a_data      ALIAS FOR $1;
     v_return    numeric;
     v_i         integer;
     v_len       integer;
BEGIN
     v_return := 0;
     v_len := array_length(a_data, 1);
     FOR v_i IN 1..v_len
     LOOP
          v_return := v_return + a_data[v_i];
     END LOOP;
     RETURN v_return;
END
$$ LANGUAGE 'plpgsql';

 
 
-- forech를 사용하여 합계 구하기

CREATE OR REPLACE FUNCTION fn_sum(numeric[])
RETURNS numeric AS $$
DECLARE
     a_data      ALIAS FOR $1;
     v_return    numeric;
     v_val       numeric;
BEGIN
     v_return := 0;
     FOREACH v_val IN ARRAY a_data

     LOOP
          v_return := v_return + v_val;
     END LOOP;

     RETURN v_return;
END
$$ LANGUAGE 'plpgsql';


사용은 

select fn_sum('{1,2}')
 
값은 3이 되겠네요...


출처: http://acpi.tistory.com/115?category=506894 
| |





      1 page / 2 page
번 호 카테고리 제 목 이름 조회수
45 postgresql postgresql ... postgresql 14 .. postgresql.conf port 5432 .. 바다아이 1861
44 postgresql , count(*) .... 바다아이 8536
43 postgresql How to do an update + join in PostgreSQL?, 바다아이 7817
42 postgresql sequence(퀀) 바다아이 10297
41 postgresql , , , index create, , 바다아이 10273
40 postgresql postgresql log_timezone .... 바다아이 8308
39 postgresql postgresql SEQUENCE reset .... 바다아이 10244
38 postgresql [PostgreSql] WITH , , Operator 바다아이 9466
현재글 postgresql postgresql for, foreach , 바다아이 11249
36 postgresql postgresql , , into ... 바다아이 11379
35 postgresql postgresql PL/pgSQL - SQL Procedural Language, , 바다아이 12954
34 postgresql postgresql ... .. , , 바다아이 14515
33 postgresql postgresql CSV export/import 바다아이 10617
32 postgresql postgresql tablespace , .... 바다아이 15840
31 postgresql postgresql 10 partitioning, ... , ... 바다아이 11160
30 postgresql Using PostgreSQL Arrays, ... ... 바다아이 11201
29 postgresql PostgreSQL (TRIGGER) (function) 바다아이 10754
28 postgresql Optimize and Improve PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX 바다아이 11388
27 postgresql postgresql tuple . vacuumdb .. , . 바다아이 10973
26 postgresql postgresql , .. 바다아이 11176
25 postgresql postgresql , size, 바다아이 13095
24 postgresql postgresql , , .... 바다아이 9935
23 postgresql PostgreSQL Replication, , , master, slave 바다아이 13533
22 postgresql postgresql case 바다아이 10000
21 postgresql postgresql with 바다아이 10438
20 postgresql postgresql , , string 바다아이 14414
19 postgresql Postgresql partitioning table , , , 바다아이 10806
18 postgresql PostgreSQL 바다아이 12447
17 postgresql postgresql vacuumdb, psql, pg_dump password crontab , pgpass 바다아이 12430
16 postgresql postgresql sequence 퀀 auto_increment . 바다아이 11376
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.07883
to webmaster... gogo sea. gogo sea.